package cn.pegasus.teamwork.pegasus.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import cn.iyizhan.teamwork.base.util.PageObject;
import cn.iyizhan.teamwork.base.util.ParameterEunm;
import cn.iyizhan.teamwork.base.util.ReMessage;
import cn.pegasus.teamwork.manager.controller.BaseManagerController;
import cn.pegasus.teamwork.pegasus.entity.BussServiceClassic;
import cn.pegasus.teamwork.pegasus.entity.BussServicePageage;
import cn.pegasus.teamwork.pegasus.mapper.BussServicePageageMapper;
import cn.pegasus.teamwork.pegasus.service.BussServiceClassicService;
import cn.pegasus.teamwork.pegasus.service.BussServicePageageService;
import cn.pegasus.teamwork.util.OSSClientUtil;

@Controller
@RequestMapping("/pageage")
public class PageageController extends BaseManagerController<BussServicePageage, BussServicePageageMapper>{
	
	private static final Logger logger = LoggerFactory.getLogger(PageageController.class);
	
	@Autowired
	private BussServicePageageService pageageService;
	@Autowired
	private BussServiceClassicService classicServic;
	/**
	 * 查看列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(@RequestParam Map<String, Object> map){
		ModelAndView view = new ModelAndView("classic/pageage-list");
		if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		try{
			PageObject<BussServicePageage> dicList = pageageService.PageList(map);
			view.addObject("list", dicList);
			view.addObject("map", map);
		}catch(Exception e){
			logger.error("查询服务列表信息异常",e.getMessage());
		}
		return view;
	}
	/**
	 * 修改
	 */
	@ResponseBody
	 @RequestMapping(value="/updatePageage", method = RequestMethod.POST)
	 public String updatePageage(@RequestParam Map<String, Object> map,
			 HttpServletRequest request){
       try {
    	   MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request;
       		MultipartFile file = fileRequest.getFile("file");
       		if (file != null && StringUtils.isNotBlank(file.getOriginalFilename())) {
        		//文件上传
            	OSSClientUtil util = new OSSClientUtil();
            	String pageageImages = util.uploadImg2Oss(file);
            	map.put("pageageImages", pageageImages);
        	}
    	   pageageService.updatePageage(getAdmin().getId(), map);
       } catch (Exception e) {
           logger.error("修改服务项目异常", e);
           return "1001";
       }
       return "200";
	 }
	/**
	 * 添加
	 */
	 @ResponseBody
	 @RequestMapping(value = "/addPageage", method = RequestMethod.POST)
	 public String addPageage(@RequestParam Map<String, Object> map,
			 HttpServletRequest request){
        try {
        	MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request;
        	MultipartFile file = fileRequest.getFile("file");
        	if (file != null && StringUtils.isNotBlank(file.getOriginalFilename())) {
        		//文件上传
            	OSSClientUtil util = new OSSClientUtil();
            	String pageageImages = util.uploadImg2Oss(file);
            	map.put("pageageImages", pageageImages);
        	}
        	pageageService.addPageage(getAdmin().getId(), map);
        } catch (Exception e) {
            logger.error("新增服务项目异常", e);
            return "1001";
        }
        return "200";
	 }
	/**
	 * 添加新增页面
	 */
	 @RequestMapping(value="/goAdd" ,method = {RequestMethod.GET})
	 public ModelAndView goAdd(@RequestParam(value="type",required = false) String type,
			 @RequestParam(value="id",required = false) Long id){
		 //服务分类
		 List<BussServiceClassic> list = classicServic.findAllList(null);
		 if(null == type){
			 ModelAndView view = new ModelAndView("classic/pageage-add");
			 view.addObject("list", list);
			 return view;
		 }else{
			 ModelAndView view = new ModelAndView("classic/pageage-edit");
			 BussServicePageage pageage = pageageService.findPageageInfo(id);
			 view.addObject("pageage", pageage);
			 view.addObject("list", list);
			 return view;
		 }
	 }

	 /**
	  * 删除
	  */
	 @ResponseBody
	 @RequestMapping(value="/delete")
	 public String delete(@RequestParam(value="id",required = false) Long id){
        try {
        	pageageService.delete(getAdmin().getId(),id);
        } catch (Exception e) {
            logger.error("删除区域异常", e);
            return "1001";
        }
        return "200";
	 }
}
